//
//  CJRECMainControllerViewController.m
//  CoJellyCartoon
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJRECMainControllerViewController.h"
#import "WZAFNetTool.h"
#import "CJRECMainModel.h"
#import "CJRECMainTableViewCell.h"
#import "CJRecTitleTableViewCell.h"
#import "CJPrePlayPageVIewController.h"
#import "LzwBangumiDetailVC.h"
#define RECURL @"http://app.bilibili.com/x/show/old?platform=android&device=&build=414000&access_key="

@interface CJRECMainControllerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *recTableView;
@property (nonatomic, strong) NSMutableArray *recDataArray;
@property (nonatomic, retain) CJRECMainModel *recMainModel;
@end

@implementation CJRECMainControllerViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.recDataArray = [NSMutableArray arrayWithCapacity:0];
    [self creatRecTableView];
    [self GetrecInfo];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(newinfo:) name:@"newsinfo" object:nil];
    
}
-(void)creatRecTableView{
    
   
    self.recTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -30, [UIScreen mainScreen].bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.recTableView.dataSource = self;
    self.recTableView.delegate = self;
    [self.recTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.recTableView registerClass:[CJRecTitleTableViewCell class] forCellReuseIdentifier:@"CJRecTitleTableViewCell"];
      [self.recTableView registerClass:[CJRECMainTableViewCell class] forCellReuseIdentifier:@"CJRECMainTableViewCell"];
    [self.view addSubview:self.recTableView];
    self.recTableView.autoresizingMask =UIViewAutoresizingFlexibleHeight;

    
    [GiFHUD show];
    [GiFHUD setGifWithImageName:@"hold.gif"];
    
  
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.recDataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return 2;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        CJRecTitleTableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"CJRecTitleTableViewCell"];
        CJRECMainModel *mainModel = self.recDataArray[indexPath.section];
        titleCell.headModel = mainModel.recHead;
        return titleCell;
    }
    
    CJRECMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJRECMainTableViewCell"];
    CJRECMainModel *mainModel = self.recDataArray[indexPath.section];
    cell.recDataArray = mainModel.body;
    return cell;
    
}

/**
 *  数据请求
 */
-(void)GetrecInfo{
    
    [WZAFNetTool GetNetWithURL:RECURL WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        for (NSDictionary *dic in reuslt[@"result"]) {
            self.recMainModel = [[CJRECMainModel alloc] initWithDictionary:dic];
            
            if ([dic[@"type"] isEqualToString:@"recommend"]|| [dic[@"type"] isEqualToString:@"bangumi_2"] ||
                [dic[@"type"] isEqualToString:@"region"]) {
                
                [self.recDataArray addObject:self.recMainModel];
            }
            
        }
        [self.recTableView reloadData];
        [GiFHUD dismiss];
    } failure:^(NSError *error) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 50;
    }
    return ([UIScreen mainScreen].bounds.size.height - 60) /2;
}

-(void)newinfo:(NSNotification *)message{
    CJRECInBodyModel *model = message.userInfo[@"model"];
    //(@"%@",model.param);
    if (model.param.length == 4) {
        LzwBangumiDetailVC *bangumidetailVC = [[LzwBangumiDetailVC alloc] init];
        bangumidetailVC.ting_ID = model.param.integerValue;
        [self.navigationController pushViewController:bangumidetailVC animated:YES];
    }else{
    CJPrePlayPageVIewController *preplayVC = [[CJPrePlayPageVIewController alloc] init];
    preplayVC.idString = model.param;
    preplayVC.picURL = model.cover;
        preplayVC.headtitle = model.title;
        //(@"1234%@", model.title);
        [self.navigationController pushViewController:preplayVC animated:YES];}
    //(@"传入");
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
